var db = require('../dbutil/index');
var sql = require('../dbutil/sql');

module.exports={
    showgoods:(req,res)=>{ 
        db.query(sql.admin.showgoods,[],(err,results)=>{ 
            if(err){
                res.send({ code: 500, message: 'sql执行错误'});
            }else{
                res.send({ code: 200, message: 'ok', data:results }); //结果封装在一个data对象中返给客户端
            }
        })
    },
    addgoods: (req, res) => { 
        let name = req.body.name; //处理和保存前端传来的商品信息
        let totalnum = req.body.totalnum;
        let price = req.body.price;
        let msg = req.body.msg;
        let inventory = req.body.inventory;
        let cid = req.body.cid;
        let shelves = req.body.shelves;
        //console.log(req.file)
        let images = req.file.filename; //获取上传图片的文件名，并将这些数据保存到数据库中。
        db.query(sql.admin.addgoods, [images, name, totalnum, price, msg, inventory, cid,shelves], (err2, results2) => {
            if (err2) {
                res.send({ code: 500, message: 'sql执行错误' });
            } else {
                res.send({ code: 200, message: 'ok', data: results2 });
            }
        });
    },
    editgoods: (req, res) => {
        console.log("req.body为：" + JSON.stringify(req.body)); 
        var img;
        let images = req.file ? req.file.filename : null; //req.file是否存在？文件已上传：文件未上传
        console.log(images);
        if(images == null){ //文件未上传
            img = req.body.img;
        }else{ //已上传文件
            img =  images;
        }
        let name = req.body.name;
        let totalnum = req.body.totalnum;
        let price = req.body.price;
        let msg = req.body.msg;
        let inventory = req.body.inventory;
        let cid = req.body.cid;
        let gid = req.body.gid;
        // console.log(name);
        db.query(sql.admin.editgoods, [img, name, totalnum, price, msg, inventory,cid,gid], (err3, results3) => {
            if (err3) {
                res.send({ code: 500, message: 'sql执行错误' });
            } else {
                res.send({ code: 200, message: 'ok', data: results3 });
            }
        });
    },
    Offshelf:(req,res)=>{
        let gid = req.body.gid;
        db.query(sql.admin.Offshelf, [gid, gid], (err, results) => {
            if (err) {
                // 如果发生错误，回滚事务
                return res.send({ code: 500, message: 'SQL执行错误' });
            }
            res.send({ code: 200, message: '商品已下架' });
        });

    },
    Onshelf:(req,res)=>{
        let gid = req.body.gid;
        db.query(sql.admin.Onshelf, [gid, gid], (err, results) => {
            if (err) {
                // 如果发生错误，回滚事务
                return res.send({ code: 500, message: 'SQL执行错误' });
            }
            res.send({ code: 200, message: '商品已上架' });
        });

    },
    showusers:(req,res)=>{ 
        db.query(sql.admin.showusers,[],(err,results)=>{ 
            if(err){
                res.send({ code: 500, message: 'sql执行错误'});
            }else{
                res.send({ code: 200, message: 'ok', data:results });
            }
        })
    },
    showtrueorder:(req,res)=>{ 
        db.query(sql.admin.showtrueorder,[],(err,results)=>{ 
            if(err){
                res.send({ code: 500, message: 'sql执行错误'});
            }else{
                res.send({ code: 200, message: 'ok', data:results });
            }
        })
    },
    showfalseorder:(req,res)=>{ 
        db.query(sql.admin.showfalseorder,[],(err,results)=>{ 
            if(err){
                res.send({ code: 500, message: 'sql执行错误'});
            }else{
                res.send({ code: 200, message: 'ok', data:results });
            }
        })
    },
    ConfirmShipment:(req,res)=>{ //确认发货
        let orderid = req.body.orderid;
        db.query(sql.admin.ConfirmShipment, [orderid, orderid], (err, results) => {
            if (err) {
                // 如果发生错误，回滚事务
                return res.send({ code: 500, message: 'SQL执行错误' });
            }
            res.send({ code: 200, message: '商品已发货' });
        });

    },
    yizhifu:(req,res)=>{  //已支付数量
        db.query(sql.admin.yizhifu,[],(err,results)=>{ 
            if(err){
                res.send({ code: 500, message: 'sql执行错误'});
            }else{
                res.send({ code: 200, message: 'ok', data:results });
            }
        })
    },
    weizhifu:(req,res)=>{  //未支付数量
        db.query(sql.admin.weizhifu,[],(err,results)=>{ 
            if(err){
                res.send({ code: 500, message: 'sql执行错误'});
            }else{
                res.send({ code: 200, message: 'ok', data:results });
            }
        })
    },
    userstatistics:(req,res)=>{  //用户数量统计
        db.query(sql.admin.userstatistics,[],(err,results)=>{ 
            if(err){
                res.send({ code: 500, message: 'sql执行错误'});
            }else{
                res.send({ code: 200, message: 'ok', data:results });
            }
        })
    },
    salestatistics:(req,res)=>{  //销售额统计
        db.query(sql.admin.salestatistics,[],(err,results)=>{ 
            if(err){
                res.send({ code: 500, message: 'sql执行错误'});
            }else{
                res.send({ code: 200, message: 'ok', data:results });
            }
        })
    },
    echone:(req,res)=>{  //各分类商品数量 echarts
        db.query(sql.admin.echone,[],(err,results)=>{ 
            if(err){
                res.send({ code: 500, message: 'sql执行错误'});
            }else{
                res.send({ code: 200, message: 'ok', data:results });
            }
        })
    },
    echtwo:(req,res)=>{  //各分类商品数量 echarts
        db.query(sql.admin.echtwo,[],(err,results)=>{ 
            if(err){
                res.send({ code: 500, message: 'sql执行错误'});
            }else{
                res.send({ code: 200, message: 'ok', data:results });
            }
        })
    },
    echthree:(req,res)=>{  //下单的各分类商品数量 echarts
        db.query(sql.admin.echthree,[],(err,results)=>{ 
            if(err){
                res.send({ code: 500, message: 'sql执行错误'});
            }else{
                res.send({ code: 200, message: 'ok', data:results });
            }
        })
    },
    
}